package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.UserBean;
import com.project.mapper.IUserMapper;
import com.project.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.List;

@Service
@Transactional
public class UserServiceImpl implements IUserService {
    @Autowired
    private IUserMapper mapper;
    @Override
    public void add(UserBean user) {
        mapper.insert(user);
    }

    @Override
    public void del(int id) {
        mapper.deleteById(id);
    }

    @Override
    public void update(int id, String pwd) {
        UserBean user = mapper.selectById(id);
        user.setPwd(pwd);
        mapper.updateById(user);
    }

    @Override
    public UserBean findById(int id) {
        return mapper.selectById(id);
    }

    @Override
    public List<UserBean> findAll() {
        return mapper.selectList(null);
    }

    @Override
    public List<UserBean> findByName(String name) {
        QueryWrapper<UserBean> qw = new QueryWrapper<>();
        qw.like("u_name",name);
        return mapper.selectList(qw);
    }

    @Override
    public List<UserBean> searchName(String name) {
        return mapper.searchName(name);
    }

    @Override
    public List<UserBean> findByItem(String name, LocalDate startDate, LocalDate endDate) {
        QueryWrapper<UserBean> qw = new QueryWrapper<>();
        if(name != null && name.length() != 0){
            qw.like("u_name",name);
        }
        if(startDate != null){
            qw.ge("u_birthday",startDate);
        }
        if(endDate != null){
            qw.le("u_birthday",endDate);
        }

        return mapper.selectList(qw);
    }

    @Override
    public IPage<UserBean> cutAll(int pageNO) {
        return mapper.selectPage(
                new Page<UserBean>(pageNO,PAGESIZE),
                null);
    }

    @Override
    public IPage<UserBean> cutByItem(int pageNO, String name, Integer startMoney, Integer endMoney) {
        QueryWrapper<UserBean> qw = new QueryWrapper<>();
        if(name != null && name.length() != 0){
            qw.like("u_name",name);
        }
        if(startMoney != null){
            qw.ge("u_money",startMoney);
        }
        if(endMoney != null){
            qw.le("u_money",endMoney);
        }

        return mapper.selectPage(new Page<UserBean>(pageNO,PAGESIZE),qw);
    }
}
